package org.gushi.datastruct.a04.list;

import java.util.Arrays;

/**
 * 改为范型对象   clear，remove方法发生改变  indexOf
 */
public class ArrayList<E> implements List<E> {

    private int size;

    private E[] elements;

    private static final int DEFAULT_CAPACITY = 10;
    private static final int ELEMENT_NOT_FOUND = -1;


    public ArrayList(int capacity) {
        capacity = capacity > DEFAULT_CAPACITY ? capacity : DEFAULT_CAPACITY;
       //对象数组
        this.elements = (E[]) new Object[capacity];
    }

    public ArrayList() {
        this(DEFAULT_CAPACITY);
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void add(E element) {
        if(element==null) return;
//        elements[size] = element;
//        size++;

//        elements[size++] = element;

        add(size, element);
    }

    @Override
    public void add(int index, E element) {
        if(element==null) return;
        rangeCheckForAdd(index);

        ensureCapacity(size + 1);

        //先移动
        for (int i = size; i > index; i--) {
            elements[i] = elements[i - 1];
        }
        //在赋值
        elements[index] = element;

        size++;

    }

    private void ensureCapacity(int capacity) {
        int oldCapacity = elements.length;
        if (oldCapacity >= capacity) return;

        int newCapacity = oldCapacity + (oldCapacity >> 1);//后面一个*1.5 扩容为以前的1.5倍
        E[] newElements = (E[]) new Object[newCapacity];
        //此处存在线程安全问题的
        for (int i = 0 ;i<size;i++){
            newElements[i] = elements[i];
        }
        elements = newElements;
        System.out.println("旧容量："+oldCapacity+"，新容量："+newCapacity);
    }
    @Override
    public E get(int index) {
        rangeCheck(index);
        return elements[index];
    }

    //根据索引更新
    @Override
    public E set(int index, E element) {
        if(element==null) return null;
        rangeCheck(index);
        E old = elements[index];
        elements[index] = element;
        return old;
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);

        E old = elements[index];

        for (int i = index; i < size; i++) {
            elements[i] = elements[i + 1];
        }
        elements[--size] = null;

        return old;
    }
    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean isContains(E element) {
        return ELEMENT_NOT_FOUND != indexOf(element);
    }

    @Override
    public int indexOf(E element) {
        if(element==null){
            for (int i = 0; i < size; i++) {
                if (elements[i].equals(null) ) {
                    return i;
                }
            }
        }else {
            for (int i = 0; i < size; i++) {
                if (element.equals(elements[i]) ) {
                    return i;
                }
            }
        }

        return ELEMENT_NOT_FOUND;
    }

    @Override
    public void clear() {
        size = 0;
        elements = null;
    }

    @Override
    public String toString() {
        return "ArrayList{" +
                "size=" + size +
                ", elements=" + Arrays.toString(elements) +
                '}';
    }


    private void outOfBounds(int index) {
        throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size);
    }

    private void rangeCheck(int index) {
        if (index < 0 || index >= size) {
            outOfBounds(index);
        }
    }

    private void rangeCheckForAdd(int index) {
        if (index < 0 || index > size) {
            outOfBounds(index);
        }
    }


}
